<?php

namespace App\Jobs;

use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Wen\Utils\CommentUtils;
use App\Wen\Utils\UserUtils;
use App\Wen\Utils\Utils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImitateCommentlikesStepJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $comment_id = 0;
    protected $need_like = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($comment_id, $need_like)
    {
        //
        $this->comment_id = $comment_id;
        $this->need_like = $need_like;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        global $__MINI_GLOBAL_PAYCODE_9__;
        if(!$__MINI_GLOBAL_PAYCODE_9__){
            return;
        }

        if( $this->need_like <= 0 ){
            return;
        }

        global $__MINI_GLOBAL_REQUEST_ENV__;
        $__MINI_GLOBAL_REQUEST_ENV__ = 'api';

        $comment = WxComment::find($this->comment_id);
        if(!$comment){
            return;
        }
        if($comment->comment_state != 1){
            return;
        }

        if(WxPost::where('id', $comment->posts_id)->where('is_examine', 1)->where('posts_state', 0)->exists()){
            $uid = UserUtils::get_a_random_robot_uid();
            if($uid > 0){
                $comment_author_id = WxComment::where('id', $this->comment_id)->value('user_id');

                global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
                if($comment_author_id > 0 && in_array($comment_author_id, explode(',', env('APP_SUPER_ADMIN_USER_ID', '')))){
                    $__MINI_GLOBAL_IS_ADMIN_SUPER__ = true;
                }
                CommentUtils::comment_like($uid, $this->comment_id);
                $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;


                if($comment_author_id > 0){
                    ImitateUserFollowsStepJob::dispatch($comment_author_id, mini_rand(0, 2))->delay(now()->addMinutes(mini_rand(1, 20)));
                }

                ImitateCommentlikesStepJob::dispatch($this->comment_id, ($this->need_like - 1))->delay(now()->addMinutes(mini_rand(6, 30)));
            }
        }
    }
}
